package com.turing.post.capitalaccounts.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "capitalaccounts.impl.BankAccountDaoImpl")
@Remote
@Local
public class BankAccountDaoImpl implements BankAccountDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public BankAccount getBankAccountByBankAccountId(BigInteger bankAccountId) {
		return _em.find(BankAccount.class, bankAccountId);
	}

	@Override
	public void insertBankAccount(PCPSWebInput webInput,BankAccount bankAccount)
			throws PCPSApplicationException {
		if(webInput==null||PCPSUtil.isEmpty(webInput.getOperBranchNo())){
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "登录用户归属机构号为空!");
		}
		String issueCompanyNo=PCPSUtil.getIssueCompanyNo(webInput);
		bankAccount.setIssueCompanyNo(issueCompanyNo);
		bankAccount.setBankAccountId(pcpsCommonDaoImpl.getUniqueIntegerKey());
		try {
			GeneralDao.insert(_em, bankAccount);
		} catch (Exception e) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "银行帐户新增失败!");
		}
	}

	@Override
	public void deleteBankAccountByBankAccountId(BigInteger bankAccountId)
			throws PCPSApplicationException {
		String sql = "delete from BankAccount where BankAccountId="
				+ bankAccountId;
		Query query = _em.createNativeQuery(sql);
		try {
			query.executeUpdate();
		} catch (Exception e) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "删除银行帐户信息失败!");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankAccount> getBankAccountListByCondition(
			PCPSWebInput webInput, String bankName, String accountName,
			String accountNo, String bankType, Integer page) {
		if (webInput.getOperBranchNo() == null) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select t.* from BankAccount t where issueCompanyNo='"
				+ issueCompanyNo + "' and BankType='" + bankType + "'";
		// 开户行名称
		if (!PCPSUtil.isEmpty(bankName)) {
			sql = sql + " and t.BankName like'%" + bankName + "%'";
		}
		// 帐户名称
		if (!PCPSUtil.isEmpty(accountName)) {
			sql = sql + " and t.AccountName like'%" + accountName + "%'";
		}
		// 帐号
		if (!PCPSUtil.isEmpty(accountNo)) {
			sql = sql + " and t.AccountNo like'%" + accountNo + "%'";
		}
		sql = sql + " order by BankAccountId DESC ";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, BankAccount.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getBankAccountTotalByCondition(PCPSWebInput webInput,
			String bankName, String accountName, String accountNo,
			String bankType) {
		if (webInput.getOperBranchNo() == null) {
			return BigInteger.ZERO;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select nvl(count(*),0) from BankAccount t where issueCompanyNo='"
				+ issueCompanyNo + "' and BankType='" + bankType + "'";
		// 开户行名称
		if (!PCPSUtil.isEmpty(bankName)) {
			sql = sql + " and t.BankName like'%" + bankName + "%'";
		}
		// 帐户名称
		if (!PCPSUtil.isEmpty(accountName)) {
			sql = sql + " and t.AccountName like'%" + accountName + "%'";
		}
		// 帐号
		if (!PCPSUtil.isEmpty(accountNo)) {
			sql = sql + " and t.AccountNo like'%" + accountNo + "%'";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void updateBankAccount(PCPSWebInput webInput,BankAccount bankAccount)
			throws PCPSApplicationException {
		if(webInput==null||PCPSUtil.isEmpty(webInput.getOperBranchNo())){
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "登录用户归属机构号为空!");
		}
		String issueCompanyNo=PCPSUtil.getIssueCompanyNo(webInput);
		try {
			BankAccount ba = getBankAccountByBankAccountId(bankAccount
					.getBankAccountId());
			ba.setIssueCompanyNo(issueCompanyNo);
			ba.setBankName(bankAccount.getBankName());
			ba.setAccountName(bankAccount.getAccountName());
			ba.setAccountNo(bankAccount.getAccountNo());
			ba.setRemark(bankAccount.getRemark());
			GeneralDao.update(_em, ba);
		} catch (Exception e) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "银行帐户修改失败!");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankAccount> getBankAccountDropdown(PCPSWebInput webInput,
			String bankType) {
		if (webInput == null || PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		String sql = "select b.* from BankAccount b where b.IssueCompanyNo='"
				+ issueCompanyNo + "' and bankType='" + bankType
				+ "' order by b.bankAccountId";
		Query query = _em.createNativeQuery(sql, BankAccount.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankAccount> getBankAccountByAccountNo(String accountNo) {
		String sql = "SELECT B.* FROM BANKACCOUNT B WHERE B.ACCOUNTNO = ?1";
		Query query = _em.createNativeQuery(sql, BankAccount.class);
		query.setParameter(1, accountNo);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BankAccount> getBankAccountByIssueCompanyNo(
			String issueCompanyNo) {
		String sql = "SELECT B.* FROM BANKACCOUNT B WHERE B.ISSUECOMPANYNO = ?1";
		Query query = _em.createNativeQuery(sql, BankAccount.class);
		query.setParameter(1, issueCompanyNo);
		return query.getResultList();
	}
}